<template>
  <!-- 用户列表模块 -->
  <div class="orders">
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区域 -->
    <el-card>
      <!-- 搜索区域 -->
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input
            placeholder="请输入搜索内容"
            v-model="queryInfo.query"
            clearable
            @clear="handleClearAble"
          >
            <el-button slot="append" icon="el-icon-search" @click="queryOrdersList"></el-button>
          </el-input>
        </el-col>
      </el-row>
      <!-- 表格区域 -->
      <el-table :data="ordersListTable" border stripe>
        <el-table-column type="index" align="center"></el-table-column>
        <el-table-column label="订单编号" prop="order_id" align="center"></el-table-column>
        <el-table-column label="订单价格" prop="order_price" align="center"></el-table-column>
        <el-table-column label="是否付款" align="center">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.pay_status === 1" size="mini" type="success">已付款</el-tag>
            <el-tag size="mini" type="danger">未付款</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="是否发货" prop="is_send" align="center"></el-table-column>
        <el-table-column label="下单时间" prop="create_time" align="center"></el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" size="mini" @click="editOrders()"></el-button>
            <el-button
              type="success"
              icon="el-icon-location"
              size="mini"
              @click="reviewOrdersById(scope.row.order_id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[2, 3, 10, 20]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>
    <!-- 编辑订单的dialog对话框 -->
    <el-dialog title="修改地址" width="50%" :visible.sync="editOrdersDialogVisiable" @close="handleEditClose">
      <!-- 修改订单的表单区域 -->
      <el-form
        :model="editOrdersForm"
        ref="editOrdersFormRef"
        :rules="editOrdersFormRules"
        label-width="100px"
      >
        <el-form-item label="省市区/县" prop="selectedCity">
          <el-cascader
            v-model="editOrdersForm.selectedCity"
            :options="cityOptions"
            :props="cityOptionsProps"
          ></el-cascader>
        </el-form-item>
        <el-form-item label="详细地址" prop="address">
          <el-input v-model="editOrdersForm.address" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="editOrdersDialogVisiable = false">取 消</el-button>
        <el-button type="primary" @click="handleEditOrdersClick">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 查看物流信息对话框-->
    <el-dialog title="物流进度" :visible.sync="timelineDialogVisible">
      <!-- 物流进度线 -->
      <el-timeline>
        <el-timeline-item
          v-for="(item, index) in logisticsInfo"
          :key="index"
          :timestamp="item.time"
        >{{item.context}}</el-timeline-item>
      </el-timeline>
    </el-dialog>
  </div>
</template>

<script>
import cityOptions from '@/assets/js/citydata.js'
export default {
  data() {
    return {
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      ordersListTable: [],
      total: 0,
      editOrdersDialogVisiable: false,
      editOrdersForm: {
        selectedCity: [],
        address: ''
      },
      editOrdersFormRules: {
        selectedCity: [
          { required: true, message: '请选择省市区/县', trigger: 'blur' }
        ],
        address: [
          { required: true, message: '请输入详细地址', trigger: 'blur' }
        ]
      },
      cityOptions,
      cityOptionsProps: {
        label: 'label',
        value: 'value',
        children: 'children',
        expandTrigger: 'hover'
      },
      logisticsInfo: [],
      timelineDialogVisible: false
    }
  },
  created() {
    this.getOrdersList()
  },
  methods: {
    async getOrdersList() {
      const { data: res } = await this.$axios.get(`orders`, {
        params: this.queryInfo
      })
      if (res.meta.status !== 200) {
        return this.$message.error('获取订单列表数据失败！！！')
      }
      this.ordersListTable = res.data.goods
      this.total = res.data.total
    },
    handleClearAble() {
      this.getOrdersList()
    },
    queryOrdersList() {
      this.getOrdersList()
    },
    editOrders() {
      this.editOrdersDialogVisiable = true
    },
    async reviewOrdersById(id) {
      const { data: res } = await this.$axios.get(`/kuaidi/804909574412544580`)
      if (res.meta.status !== 200) {
        return this.$message.error('获取物流信息失败!!!')
      }
      this.logisticsInfo = res.data
      this.timelineDialogVisible = true
    },
    handleSizeChange(newSize) {
      this.queryInfo.pagenum = 1
      this.queryInfo.pagesize = newSize
      this.getOrdersList()
    },
    handleCurrentChange(newPage) {
      this.queryInfo.pagenum = newPage
      this.getOrdersList()
    },
    handleEditClose() {
      this.$refs['editOrdersFormRef'].resetFields()
    },
    handleEditOrdersClick() {
      this.editOrdersDialogVisiable = false
    }
  }
}
</script>

<style lang="less">
.orders {
  .el-cascader {
    width: 100%;
  }
}
</style>
